import { MockMethod } from 'vite-plugin-mock'
import { Role, Permission } from '@/types/role'
import { PaginationData } from '@/types'

// 模拟角色数据
const roles: Role[] = [
  {
    id: 1,
    name: '超级管理员',
    code: 'SUPER_ADMIN',
    description: '系统最高权限，可以操作所有功能',
    createTime: '2025-01-01 12:00:00',
    updateTime: '2025-01-01 12:00:00',
    status: '启用'
  },
  {
    id: 2,
    name: '管理员',
    code: 'ADMIN',
    description: '系统管理员，可以操作大部分功能',
    createTime: '2025-01-02 10:30:00',
    updateTime: '2025-01-02 10:30:00',
    status: '启用'
  },
  {
    id: 3,
    name: '编辑',
    code: 'EDITOR',
    description: '内容编辑，负责内容管理',
    createTime: '2025-01-03 15:20:00',
    updateTime: '2025-01-03 15:20:00',
    status: '启用'
  },
  {
    id: 4,
    name: '普通用户',
    code: 'USER',
    description: '普通用户，基础功能访问权限',
    createTime: '2025-01-04 09:15:00',
    updateTime: '2025-01-04 09:15:00',
    status: '启用'
  },
  {
    id: 5,
    name: '访客',
    code: 'GUEST',
    description: '访客角色，只读权限',
    createTime: '2025-01-05 14:40:00',
    updateTime: '2025-01-05 14:40:00',
    status: '禁用'
  }
]

// 模拟权限树
const permissionTree: Permission[] = [
  {
    id: 1,
    name: '系统管理',
    type: 'module',
    children: [
      {
        id: 11,
        name: '用户管理',
        type: 'menu',
        parentId: 1,
        children: [
          { id: 111, name: '查看用户', type: 'button', parentId: 11 },
          { id: 112, name: '新增用户', type: 'button', parentId: 11 },
          { id: 113, name: '编辑用户', type: 'button', parentId: 11 },
          { id: 114, name: '删除用户', type: 'button', parentId: 11 }
        ]
      },
      {
        id: 12,
        name: '角色管理',
        type: 'menu',
        parentId: 1,
        children: [
          { id: 121, name: '查看角色', type: 'button', parentId: 12 },
          { id: 122, name: '新增角色', type: 'button', parentId: 12 },
          { id: 123, name: '编辑角色', type: 'button', parentId: 12 },
          { id: 124, name: '删除角色', type: 'button', parentId: 12 },
          { id: 125, name: '分配权限', type: 'button', parentId: 12 }
        ]
      },
      {
        id: 13,
        name: '权限管理',
        type: 'menu',
        parentId: 1,
        children: [
          { id: 131, name: '查看权限', type: 'button', parentId: 13 },
          { id: 132, name: '新增权限', type: 'button', parentId: 13 },
          { id: 133, name: '编辑权限', type: 'button', parentId: 13 },
          { id: 134, name: '删除权限', type: 'button', parentId: 13 }
        ]
      }
    ]
  },
  {
    id: 2,
    name: '内容管理',
    type: 'module',
    children: [
      {
        id: 21,
        name: '文章管理',
        type: 'menu',
        parentId: 2,
        children: [
          { id: 211, name: '查看文章', type: 'button', parentId: 21 },
          { id: 212, name: '新增文章', type: 'button', parentId: 21 },
          { id: 213, name: '编辑文章', type: 'button', parentId: 21 },
          { id: 214, name: '删除文章', type: 'button', parentId: 21 }
        ]
      },
      {
        id: 22,
        name: '分类管理',
        type: 'menu',
        parentId: 2,
        children: [
          { id: 221, name: '查看分类', type: 'button', parentId: 22 },
          { id: 222, name: '新增分类', type: 'button', parentId: 22 },
          { id: 223, name: '编辑分类', type: 'button', parentId: 22 },
          { id: 224, name: '删除分类', type: 'button', parentId: 22 }
        ]
      }
    ]
  },
  {
    id: 3,
    name: '数据统计',
    type: 'module',
    children: [
      { id: 31, name: '访问统计', type: 'menu', parentId: 3 },
      { id: 32, name: '用户统计', type: 'menu', parentId: 3 }
    ]
  }
]

// 角色权限映射
const rolePermissions: Record<number, number[]> = {
  1: [111, 112, 113, 114, 121, 122, 123, 124, 125, 131, 132, 133, 134, 211, 212, 213, 214, 221, 222, 223, 224, 31, 32], // 超级管理员
  2: [111, 112, 113, 121, 122, 123, 131, 132, 133, 211, 212, 213, 221, 222, 223, 31, 32], // 管理员
  3: [111, 211, 212, 213, 214, 221, 222, 223, 224, 31], // 编辑
  4: [111, 121, 131, 211, 221, 31], // 普通用户
  5: [111, 211, 221] // 访客
}

export default [
  // 获取角色列表
  {
    url: '/api/system/roles',
    method: 'get',
    response: (request: any) => {
      const { page = 1, pageSize = 10, keyword = '' } = request.query
      
      let filteredRoles = roles
      
      // 搜索过滤
      if (keyword) {
        const keywordLower = keyword.toLowerCase()
        filteredRoles = roles.filter(
          role => role.name.toLowerCase().includes(keywordLower) || 
                 role.code.toLowerCase().includes(keywordLower)
        )
      }
      
      // 分页
      const startIndex = (page - 1) * pageSize
      const endIndex = startIndex + pageSize
      const pageRoles = filteredRoles.slice(startIndex, endIndex)
      
      return {
        code: 200,
        message: 'success',
        data: {
          list: pageRoles,
          total: filteredRoles.length,
          page: Number(page),
          pageSize: Number(pageSize)
        } as PaginationData<Role>
      }
    }
  },
  
  // 获取角色详情
  {
    url: '/api/system/roles/:id',
    method: 'get',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少角色ID参数',
          data: null
        }
      }
      
      const role = roles.find(role => role.id === Number(id))
      
      if (!role) {
        return {
          code: 404,
          message: '角色不存在',
          data: null
        }
      }
      
      return {
        code: 200,
        message: 'success',
        data: role
      }
    }
  },
  
  // 创建角色
  {
    url: '/api/system/roles',
    method: 'post',
    response: (request: any) => {
      const roleData = request.body
      
      // 验证角色编码唯一性
      const existingRole = roles.find(role => role.code === roleData.code)
      if (existingRole) {
        return {
          code: 400,
          message: '角色编码已存在',
          data: null
        }
      }
      
      // 创建新角色
      const newRole: Role = {
        id: roles.length + 1,
        name: roleData.name,
        code: roleData.code,
        description: roleData.description || '',
        status: roleData.status || '启用',
        createTime: new Date().toLocaleString(),
        updateTime: new Date().toLocaleString()
      }
      
      roles.push(newRole)
      
      return {
        code: 200,
        message: '创建成功',
        data: newRole
      }
    }
  },
  
  // 更新角色
  {
    url: '/api/system/roles/:id',
    method: 'put',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少角色ID参数',
          data: null
        }
      }
      
      const roleData = request.body
      const roleIndex = roles.findIndex(role => role.id === Number(id))
      
      if (roleIndex === -1) {
        return {
          code: 404,
          message: '角色不存在',
          data: null
        }
      }
      
      // 更新角色
      const updatedRole: Role = {
        ...roles[roleIndex],
        name: roleData.name,
        description: roleData.description || roles[roleIndex].description,
        status: roleData.status || roles[roleIndex].status,
        updateTime: new Date().toLocaleString()
      }
      
      roles[roleIndex] = updatedRole
      
      return {
        code: 200,
        message: '更新成功',
        data: updatedRole
      }
    }
  },
  
  // 删除角色
  {
    url: '/api/system/roles/:id',
    method: 'delete',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少角色ID参数',
          data: null
        }
      }
      
      const roleIndex = roles.findIndex(role => role.id === Number(id))
      
      if (roleIndex === -1) {
        return {
          code: 404,
          message: '角色不存在',
          data: null
        }
      }
      
      // 禁止删除超级管理员
      if (roles[roleIndex].code === 'SUPER_ADMIN') {
        return {
          code: 403,
          message: '无法删除超级管理员',
          data: null
        }
      }
      
      // 删除角色
      const deletedRole = roles.splice(roleIndex, 1)[0]
      
      return {
        code: 200,
        message: '删除成功',
        data: deletedRole
      }
    }
  },
  
  // 更新角色状态
  {
    url: '/api/system/roles/:id/status',
    method: 'patch',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少角色ID参数',
          data: null
        }
      }
      
      const { status } = request.body
      const roleIndex = roles.findIndex(role => role.id === Number(id))
      
      if (roleIndex === -1) {
        return {
          code: 404,
          message: '角色不存在',
          data: null
        }
      }
      
      // 禁止禁用超级管理员
      if (roles[roleIndex].code === 'SUPER_ADMIN' && status === '禁用') {
        return {
          code: 403,
          message: '无法禁用超级管理员',
          data: null
        }
      }
      
      // 更新状态
      roles[roleIndex].status = status
      roles[roleIndex].updateTime = new Date().toLocaleString()
      
      return {
        code: 200,
        message: '更新成功',
        data: roles[roleIndex]
      }
    }
  },
  
  // 获取角色权限
  {
    url: '/api/system/roles/:id/permissions',
    method: 'get',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少角色ID参数',
          data: null
        }
      }
      
      const roleId = Number(id)
      
      if (!roles.some(role => role.id === roleId)) {
        return {
          code: 404,
          message: '角色不存在',
          data: null
        }
      }
      
      return {
        code: 200,
        message: 'success',
        data: rolePermissions[roleId] || []
      }
    }
  },
  
  // 更新角色权限
  {
    url: '/api/system/roles/:id/permissions',
    method: 'put',
    response: (request: any) => {
      const id = request.params?.id || request.query?.id
      if (!id) {
        return {
          code: 400,
          message: '缺少角色ID参数',
          data: null
        }
      }
      
      const { permissionIds } = request.body
      const roleId = Number(id)
      
      if (!roles.some(role => role.id === roleId)) {
        return {
          code: 404,
          message: '角色不存在',
          data: null
        }
      }
      
      // 更新权限
      rolePermissions[roleId] = permissionIds
      
      return {
        code: 200,
        message: '权限更新成功',
        data: permissionIds
      }
    }
  },
  
  // 获取权限树
  {
    url: '/api/system/permissions/tree',
    method: 'get',
    response: () => {
      return {
        code: 200,
        message: 'success',
        data: permissionTree
      }
    }
  }
] as MockMethod[] 